package chatuidemo.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.BaseActivity;
import com.ysm.smfu.bean.Friend;
import com.ysm.smfu.bean.Friends;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.manager.GlideCircleTransform;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import easeui.widget.EaseAlertDialog;


/**
 * Created by Administrator on 2016/10/13.
 */
public class ZHSearchActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.query_edit)
    EditText queryEdit;
    @BindView(R.id.search_clear)
    ImageButton searchClear;
    @BindView(R.id.query)
    TextView query;
    @BindView(R.id.listView)
    ListView listView;
    private List<Friend> friends = new ArrayList<>();
    private MyAdapter adapter;


    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.em_activity_search);
        ButterKnife.bind(this);
    }

    /**
     * 设置数据
     */
    protected void initData() {
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ZHSearchActivity.this, AddFriendsActivity.class);
                intent.putExtra(SpKey.USERID, friends.get(position).id);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 初始化和设置监听
     */
    protected void initListener() {

        query.setOnClickListener(this);
        queryEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    searchClear.setVisibility(View.VISIBLE);
                } else {
                    searchClear.setVisibility(View.GONE);
                }
            }
        });

        searchClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                queryEdit.setText("");
            }
        });
    }


    /**
     * 搜索好友
     * http://192.168.1.76:8090/hltx-web/app/user/getByPhone
     * "phone: 用户手机(账号)
     * petName:用户昵称"
     *
     * @param v
     */

    @Override
    public void onInnerClick(View v) {
        super.onInnerClick(v);
        String userName = queryEdit.getText().toString().trim();
        if (TextUtils.isEmpty(userName)) {
            new EaseAlertDialog(this, R.string.Please_enter_a_username).show();
            return;
        }

        BaseProtocal.builder(true).getContactByPhone(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                    PrintUtil.printRespones("好友搜索结果",response);
                Friends friendss = new Gson().fromJson(response, Friends.class);
                if (friendss != null && friendss.user != null && friendss.user.size() > 0) {
                    friends = friendss.user;
                    adapter.notifyDataSetChanged();
                } else {
//                    暂无该好友信息
                    ToastUtils.showToast("暂无该好友信息");
                }
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.showErrToast();
            }
        }, userName);
    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return friends.size();
        }

        @Override
        public Object getItem(int position) {
            return friends.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = UIUtils.inflate(R.layout.list_item_search);
                viewHolder = new ViewHolder(convertView);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            Friend friend = friends.get(position);
            if (!TextUtils.isEmpty(friend.imageAddress) && !friend.imageAddress.startsWith("null?"))
                Glide.with(ZHSearchActivity.this).load(friend.imageAddress).placeholder(R.drawable.myimg2).error(R.drawable.myimg2)
                        .transform(new GlideCircleTransform(UIUtils.getContext()))
                        .into(viewHolder.img);
            viewHolder.nc.setText(friend.petName);
            return convertView;
        }
    }

    class ViewHolder {

        ImageView img;
        TextView nc;

        public ViewHolder(View view) {
            img = (ImageView) view.findViewById(R.id.img);
            nc = (TextView) view.findViewById(R.id.nc);
            view.setTag(this);
            AutoUtils.autoSize(view);
        }
    }
}
